

import socket
import hashlib
client = socket.socket()

client.connect(('localhost', 9999))

while True:
    cmd = input('>>>').strip()
    if len(cmd)==0: continue
    if cmd.startswith('get')
        client.send(cmd.encode())
        server_res = client.recv(1024)
        print('server_res, 应该是文件大小', server_res)
        client.send(b'ready to recv')
        file_total_size = int(server_res.decode())
        received_size = 0
        file_name = cmd.split()[1]
        f = open(file_name, 'wb')
        m = hashlib.md5()

        while received_size < file_total_size:
            if file_total_size - received_size > 1024:
                size = 1024
            else:
                size = file_total_size - received_size
            data = client.recv(size)
            received_size += len(data)
            m.update(data)
            f.write(data)
            # print('received_size', received_size)
            # print('file_total_size', file_total_size)
        else:
            new_file_md5 = m.hexdigest()
            print('收完了')
            f.close()
        server_md5 = client.recv(1024)
        print('server_md5', server_md5)
        print('new_file_md5', new_file_md5)

client.close()
